#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>



typedef int QDatatype;

typedef struct QueueNode
{
	struct QueueNode* next;
	QDatatype val;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;


void QueueInit(Queue* phead);
void QueueQestroy(Queue* phead);
void QueuePush(Queue* phead, QDatatype x);
void QueuePop(Queue* phead);
bool QueueEmpty(Queue* phead);
QDatatype QueueFront(Queue* phead);
QDatatype QueueBack(Queue* phead);
int QueueSize(Queue* phead);

bool QueueEmpty(Queue* phead)
{
	//assert(phead);
	return phead->size == 0;
}

void QueueInit(Queue* phead)
{
	assert(phead);
	phead->head = phead->tail = NULL;
	phead->size = 0;
}

void QueueQestroy(Queue* phead)
{
	assert(phead);
	QNode* cur = phead->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	phead->head = NULL;
	phead->tail = NULL;
	phead->size = 0;
}
void QueuePush(Queue* pq, QDatatype x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->val = x;
	newnode->next = NULL;

	if (pq->head == NULL)
	{
		//assert(pq->tail == NULL);

		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

	pq->size++;
}

void QueuePop(Queue* phead)
{
	assert(phead);
	assert(!QueueEmpty(phead));
	QNode* head = phead->head;
	QNode* next = phead->head->next;
	free(head);
	phead->head = next;
	phead->size--;
}


QDatatype QueueFront(Queue* phead)
{
	assert(phead);
	assert(!QueueEmpty(phead));
	return phead->head->val;
}

QDatatype QueueBack(Queue* phead)
{
	assert(phead);
	assert(!QueueEmpty(phead));
	return phead->tail->val;
}

int QueueSize(Queue* phead)
{
	assert(phead);
	assert(!QueueEmpty(phead));
	return phead->size;
}

typedef struct {
	Queue p1;
	Queue p2;
} MyStack;

/////////////////

MyStack* myStackCreate() {
	MyStack* S = (MyStack*)malloc(sizeof(MyStack));
	if (S == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	QueueInit(&S->p1);
	QueueInit(&S->p2);
	return S;
}

bool myStackEmpty(MyStack* obj) {
	return QueueEmpty(&obj->p1) && QueueEmpty(&obj->p2);
}

void myStackPush(MyStack* obj, int x) {
	assert(obj);
	if (!QueueEmpty(&obj->p1))
	{
		QueuePush(&obj->p1, x);
	}
	else
	{
		QueuePush(&obj->p2, x);
	}
}

int myStackPop(MyStack* obj) {
	Queue* NullQueue = &obj->p1;
	Queue* NoneQueue = &obj->p2;
	if (!QueueEmpty(&obj->p1))
	{
		NullQueue = &obj->p2;
		NoneQueue = &obj->p1;
	}
	while (QueueSize(NoneQueue) > 1)
	{
		QueuePush(NullQueue, QueueFront(NoneQueue));
		QueuePop(NoneQueue);
	}
	int str = QueueFront(NoneQueue);
	QueuePop(NoneQueue);
	return str;
}

int myStackTop(MyStack* obj) {
	assert(obj);
	assert(!myStackEmpty(obj));
	if (!QueueEmpty(&obj->p1))
	{
		return QueueBack(&obj->p1);
	}
	else
	{
		return QueueBack(&obj->p2);
	}
}

void myStackFree(MyStack* obj) {
	QueueQestroy(&obj->p1);
	QueueQestroy(&obj->p2);
	free(obj);
}

/**
 * Your MyStack struct will be instantiated and called as such:
 * MyStack* obj = myStackCreate();
 * myStackPush(obj, x);

 * int param_2 = myStackPop(obj);

 * int param_3 = myStackTop(obj);

 * bool param_4 = myStackEmpty(obj);

 * myStackFree(obj);
*/